import os
import csv

def is_file_exist(file_path: str) -> bool:
    return os.path.exists(file_path)

# 将新的key值写入csv文件
def write_new_keys_to_csv(file_path, keys, operator="a"):
    if operator == "a" and not is_file_exist(file_path):
        return
    with open(file_path, operator) as fp:
        writer = csv.writer(fp)
        next(writer)
        for key in keys:
            writer.writerow([key, "", ""])

# 获取指定文件路径下文件的首列值
def get_csv_values(file_path: str, row_num=0) -> ():
    if not is_file_exist(file_path):
        print('file not exist')
        return ()
    if not file_path.endswith('.csv'):
        print('file not end with:' + file_path)
        return ()
    if row_num != 0 and get_csv_colum_count(file_path) <= row_num:
        return
    result_set = set()
    with open(file_path, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            if len(row) > row_num:
                result_set.add(row[row_num])
    return result_set

def get_csv_colum_count(file_path):
    if not is_file_exist(file_path):
        return
    num_of_cols = 0
    with open(file_path, 'r') as file:
        csv_file = csv.reader(file)
        header = next(csv_file)
        num_of_cols = len(header)
    return num_of_cols

